#!/usr/bin/env bash

echo "building images"
# build user rpc
echo "building user-rpc:dev image"
go mod tidy
if [[ -z $(docker ps -q -f "name=^user_rpc:dev$") ]]; then
  echo "remove user-rpc image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/user-rpc -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-user-rpc -q) &> /dev/null
  echo "old user-rpc removed"
fi
docker buildx build -t dev-user-rpc -f app/user/rpc/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-user-rpc ccr.ccs.tencentyun.com/johnson/user-rpc:dev
docker push ccr.ccs.tencentyun.com/johnson/user-rpc:dev
echo "user-rpc:dev image built"

# build user api
echo "building user-api:dev image"
if [[ -z $(docker ps -q -f "name=^user-api:dev$") ]]; then
  echo "remove user-api image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/user-api -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-user-api -q) &> /dev/null
  echo "old user-api removed"
fi
docker buildx build -t dev-user-api -f app/user/api/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-user-api ccr.ccs.tencentyun.com/johnson/user-api:dev
docker push ccr.ccs.tencentyun.com/johnson/user-api:dev
echo "user-api:dev image built"

# build portal rpc
echo "building portal-rpc:dev image"
if [[ -z $(docker ps -q -f "name=^portal-rpc:dev$") ]]; then
  echo "remove portal-rpc image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/portal-rpc -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-portal-rpc -q) &> /dev/null
  echo "old portal-rpc removed"
fi
docker buildx build -t dev-portal-rpc -f app/portal/rpc/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-portal-rpc ccr.ccs.tencentyun.com/johnson/portal-rpc:dev
docker push ccr.ccs.tencentyun.com/johnson/portal-rpc:dev
echo "portal-rpc:dev image built"

# build portal api
echo "building portal-api:dev image"
if [[ -z $(docker ps -q -f "name=^portal-api:dev$") ]]; then
  echo "remove portal-api image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/portal-api -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-portal-api -q) &> /dev/null
  echo "old portal-api removed"
fi
docker buildx build -t dev-portal-api -f app/portal/api/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-portal-api ccr.ccs.tencentyun.com/johnson/portal-api:dev
docker push ccr.ccs.tencentyun.com/johnson/portal-api:dev
echo "portal-api:dev image built"

# build portal mq
echo "building portal-mq:dev image"
if [[ -z $(docker ps -q -f "name=^portal-mq:dev$") ]]; then
  echo "remove portal-mq image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/portal-mq -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-portal-mq -q) &> /dev/null
  echo "old portal-mq removed"
fi
docker buildx build -t dev-portal-mq -f app/portal/mq/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-portal-mq ccr.ccs.tencentyun.com/johnson/portal-mq:dev
docker push ccr.ccs.tencentyun.com/johnson/portal-mq:dev
echo "portal-mq:dev image built"

# build trade rpc
echo "building trade-rpc:dev image"
if [[ -z $(docker ps -q -f "name=^trade-rpc:dev$") ]]; then
  echo "remove trade-rpc image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/trade-rpc -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-trade-rpc -q) &> /dev/null
  echo "old trade-rpc removed"
fi
docker buildx build -t dev-trade-rpc -f app/trade/rpc/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-trade-rpc ccr.ccs.tencentyun.com/johnson/trade-rpc:dev
docker push ccr.ccs.tencentyun.com/johnson/trade-rpc:dev
echo "trade-rpc:dev image built"

# build trade api
echo "building trade-api:dev image"
if [[ -z $(docker ps -q -f "name=^trade-api:dev$") ]]; then
  echo "remove trade-api image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/trade-api -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-trade-api -q) &> /dev/null
  echo "old trade-api removed"
fi
docker buildx build -t dev-trade-api -f app/trade/api/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-trade-api ccr.ccs.tencentyun.com/johnson/trade-api:dev
docker push ccr.ccs.tencentyun.com/johnson/trade-api:dev
echo "trade-api:dev image built"

# build trade mq
echo "building trade-mq:dev image"
if [[ -z $(docker ps -q -f "name=^trade-mq:dev$") ]]; then
  echo "remove trade-mq image"
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/trade-mq -q) &> /dev/null
  docker rmi -f $(docker images ccr.ccs.tencentyun.com/johnson/dev-trade-mq -q) &> /dev/null
  echo "old trade-mq removed"
fi
docker buildx build -t dev-trade-mq -f app/trade/mq/Dockerfile --build-arg BUILD_ENV=dev .
docker tag dev-trade-mq ccr.ccs.tencentyun.com/johnson/trade-mq:dev
docker push ccr.ccs.tencentyun.com/johnson/trade-mq:dev
echo "trade-mq:dev image built"

echo "images built"